import {
  constantRoutes
} from '@/router'
import api from '@/utils/api'

import Layout from '@/layout'
import {
  getToken,
  setToken,
  removeToken,
  setChatEn,
  removeChatEn
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

/**
 * 后台查询的菜单数据拼装成路由格式的数据
 * @param routes
 */
export function generaMenu(routes, data) {
  data.forEach(item => {
    const menu = {
      path: item.path,
      component: item.component_path === '' ? Layout : (resolve) => require([`@/views/${item.component_path}`], resolve),
      // hidden: true,
      children: [],
      name: item.name,
      meta: {
        title: item.title,
        icon: item.icon
      }
    }
    if (item.children) {
      generaMenu(menu.children, item.children)
    }
    routes.push(menu)
  })
}

const state = {
  token: getToken(),
  sys_title: '',
  store_title: '',
  city_title: '',
  is_console: '',
  w7copyright: '',
  logo: '',
  avatar: '',
  roles: [],
  actions: [],
  username: '',
  role_id: '',
  routes: [],
  addRoutes: {}
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ACTIONS(state, actions) {
    state.actions = actions
  },
  SET_INFO(state, val) {
    state.username = val.username
    state.role_id = val.role_id
    state.sys_title = val.sys_title
    state.store_title = val.store_title
    state.city_title = val.city_title
    state.is_console = val.is_console
    state.logo = val.logo
    state.w7copyright = val.w7copyright
  },
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }

}

const actions = {
  login({
    commit
  }, userInfo) {
    const {
      username,
      password,
      m
    } = userInfo
    return new Promise((resolve, reject) => {
      api.post('/Login/index', {
        username: username.trim(),
        password: password,
        m: m
      }).then(response => {
        commit('SET_TOKEN', response.token)
        setToken(response.token)
        setChatEn(response.data.serverChatEn)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
    commit
  }) {
    return new Promise((resolve, reject) => {
      const loadMenuData = []
      const asyncRoutes = []

      api.post('/Users/getUserInfo').then(response => {
        const data = response.data
        if (!data) {
          reject('清重新登录')
        }

        const {
          roles,
          avatar
        } = data

        // roles must be a non-empty array
        if (!roles || roles.length <= 0) {
          reject('getInfo: roles must be a non-null array!')
        }
        const menu = response.menu

        menu.push({
          "name": "attendance",
          "pid": 0,
          "id": 189,
          "title": "考勤管理",
          "sort": 50,
          "icon": "el-icon-help",
          "path": "attendance",
          "component_path": "",
          "children": [
              {
                  "name": "/config/attendance",
                  "pid": 189,
                  "id": 188,
                  "title": "考勤设置 ",
                  "sort": 90,
                  "icon": "el-icon-set-up",
                  "path": "/config/attendance",
                  "component_path": "config/attendance.vue",
                  "children": []
              },
              {
                  "name": "/attendance/index",
                  "pid": 189,
                  "id": 190,
                  "title": "打卡记录",
                  "sort": 10,
                  "icon": "el-icon-s-help",
                  "path": "/attendance/index",
                  "component_path": "attendance/index.vue",
                  "children": []
              },
              {
                  "name": "/attendance/audit",
                  "pid": 189,
                  "id": 146,
                  "title": "请休申请",
                  "sort": 20,
                  "icon": "el-icon-finished",
                  "path": "/attendance/audit",
                  "component_path": "attendance/audit.vue",
                  "children": []
              }
          ]
      })

      menu.push({
        "name": "material",
        "pid": 0,
        "id": 189,
        "title": "物料管理",
        "sort": 50,
        "icon": "el-icon-help",
        "path": "material",
        "component_path": "",
        "children": [
            {
                "name": "/material/index",
                "pid": 189,
                "id": 190,
                "title": "物料库存",
                "sort": 10,
                "icon": "el-icon-s-help",
                "path": "/material/index",
                "component_path": "material/index.vue",
                "children": []
            },
            {
                "name": "/material/use",
                "pid": 189,
                "id": 146,
                "title": "物料领用",
                "sort": 20,
                "icon": "el-icon-finished",
                "path": "/material/use",
                "component_path": "material/use.vue",
                "children": []
            }
        ]
    })

    menu.push({
      "name": "property",
      "pid": 0,
      "id": 189,
      "title": "物业账号管理",
      "sort": 50,
      "icon": "el-icon-help",
      "path": "property",
      "component_path": "",
      "children": [
          {
              "name": "/property/index",
              "pid": 189,
              "id": 190,
              "title": "账号列表",
              "sort": 10,
              "icon": "el-icon-s-help",
              "path": "/property/index",
              "component_path": "property/index.vue",
              "children": []
          }
      ]
  })

//   menu.push({
//     "name": "priceDifference",
//     "pid": 0,
//     "id": 189,
//     "title": "补差价订单",
//     "sort": 50,
//     "icon": "el-icon-help",
//     "path": "priceDifference",
//     "component_path": "",
//     "children": [
//         {
//             "name": "/order/priceDifference",
//             "pid": 189,
//             "id": 190,
//             "title": "补差价订单",
//             "sort": 10,
//             "icon": "el-icon-s-help",
//             "path": "/order/priceDifference",
//             "component_path": "order/priceDifference.vue",
//             "children": []
//         }
//     ]
// })

menu.push({
  "name": "quality",
  "pid": 0,
  "id": 189,
  "title": "品质抽查",
  "sort": 50,
  "icon": "el-icon-help",
  "path": "quality",
  "component_path": "",
  "children": [
      {
          "name": "/quality/index",
          "pid": 189,
          "id": 190,
          "title": "品质抽查",
          "sort": 10,
          "icon": "el-icon-s-help",
          "path": "/quality/index",
          "component_path": "quality/index.vue",
          "children": []
      }
  ]
})

menu.push({
  "name": "feedback",
  "pid": 0,
  "id": 189,
  "title": "意见反馈",
  "sort": 50,
  "icon": "el-icon-help",
  "path": "feedback",
  "component_path": "",
  "children": [
      {
          "name": "/feedback/index",
          "pid": 189,
          "id": 190,
          "title": "意见反馈",
          "sort": 10,
          "icon": "el-icon-s-help",
          "path": "/feedback/index",
          "component_path": "feedback/index.vue",
          "children": []
      }
  ]
})

menu.push({
  "name": "zhaoping",
  "pid": 0,
  "id": 191,
  "title": "招聘信息",
  "sort": 50,
  "icon": "el-icon-help",
  "path": "zhaoping",
  "component_path": "",
  "children": [
      {
          "name": "/zhaoping/index",
          "pid": 191,
          "id": 192,
          "title": "招聘信息",
          "sort": 10,
          "icon": "el-icon-s-help",
          "path": "/zhaoping/index",
          "component_path": "zhaoping/index.vue",
          "children": []
      }
  ]
})


        if (menu) {
          Object.assign(loadMenuData, menu)
          generaMenu(asyncRoutes, loadMenuData)
        }

        const accessedRoutes = asyncRoutes || []

        data.menu = accessedRoutes
        commit('SET_ROUTES', accessedRoutes)
        commit('SET_ROLES', roles)
        commit('SET_AVATAR', avatar)
        commit('SET_ACTIONS', response.actions)
        commit('SET_INFO', response.data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 退出
  logout({
    commit,
    state,
    dispatch
  }) {
    return new Promise((resolve, reject) => {
      api.post('/Users/logout', state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_ACTIONS', [])
        commit('SET_INFO', {})
        removeToken()
        removeChatEn()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, {
          root: true
        })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  async changeRoles({
    commit,
    dispatch
  }, role) {
    const token = role + '-token'

    commit('SET_TOKEN', token)
    setToken(token)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, {
      root: true
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
